//test
package userInterface;

import persistance.*;
import importer.*;
import converter.*;

import java.io.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.*;
import javax.swing.border.*;


public class ApplicationController extends JPanel implements ActionListener{

	/**
	 * @param args
	 * 
	 * 
	 */
	
	static final long serialVersionUID = 1;
	private static String NEW_PROJECT = "new";
	private static String OPEN_PROJECT = "open";
	private JFileChooser fc;
	private static JFrame frame;
	private JLabel Titlepicture;
	
	 public ApplicationController() {
	        super(new BorderLayout());
	        
	        Titlepicture = new JLabel(createImageIcon("splash3.gif"));
	        fc = new JFileChooser();
	        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	        JButton newButton = new JButton("New Project");
	        newButton.setActionCommand(NEW_PROJECT);
	        newButton.addActionListener(this);
	        
	        JButton openButton = new JButton("Close Application");
	        openButton.setActionCommand(OPEN_PROJECT);
	        openButton.addActionListener(this);
	        
	        JPanel panel = new JPanel(new GridLayout(0,2));
	        panel.add(newButton);
	        panel.add(openButton); 
	        add(panel, BorderLayout.SOUTH);
	        add(Titlepicture, BorderLayout.NORTH);
	        
	 }

	    public void actionPerformed(ActionEvent e) {
	        String command = e.getActionCommand();
	        
	        if (NEW_PROJECT.equals(command)) {
	                
	        	fc.setName("Please Select the Workspace Folder");
	        	int returnVal = fc.showDialog(ApplicationController.this, "Select Working Directory");
	            if (returnVal == JFileChooser.APPROVE_OPTION) {
	            	File workingDir = fc.getSelectedFile();
	            	String[] callString = new String[2];
	            	callString[0] = "caller";
	            	callString[1] = workingDir.getAbsolutePath();
	            	//projectManager = new ProjectManager(file);
	                MainApplicationFrame.create(callString);
	                frame.setVisible(false);
	            	
	            }
	        	
	        } 
	        else if (OPEN_PROJECT.equals(command)) {
	            //Remove button clicked
	            System.exit(1);
	        } 

	    }
	
	
    private static void createAndShowGUI() {
        //Create and set up the window.
        frame = new JFrame("WELCOME TO MAS UML MODELER");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        ApplicationController newContentPane = new ApplicationController();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = ApplicationController.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

	public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });

	}

}
